#!/bin/sh
#
# Start the network....
#

# Debian ifupdown needs the /run/network lock directory
mkdir -p /run/network

# Function to create network interfaces configuration file
create_network_interfaces() {
	if [ ! -f /mnt/config/interfaces ]; then
		printf "Creating /mnt/config/interfaces: "
		mkdir -p /mnt/config
		cat > /mnt/config/interfaces << 'EOF'
# Network interface configuration
# This file is located at /mnt/config/interfaces and linked to /etc/network/interfaces

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
  pre-up /etc/network/nfs_check
  wait-delay 15
  hostname $(hostname)

# Additional network interfaces can be added here
# Example for static IP configuration:
# auto eth1
# iface eth1 inet static
#   address 192.168.1.100
#   netmask 255.255.255.0
#   gateway 192.168.1.1
#   dns-nameservers 8.8.8.8 8.8.4.4
EOF
		[ $? = 0 ] && echo "OK" || echo "FAIL"
	fi
}

case "$1" in
  start)
	printf "Starting network: "
	
	# Create network interfaces configuration if needed
	create_network_interfaces
	
	/sbin/ifup -a
	[ $? = 0 ] && echo "OK" || echo "FAIL"
	;;
  stop)
	printf "Stopping network: "
	/sbin/ifdown -a
	[ $? = 0 ] && echo "OK" || echo "FAIL"
	;;
  restart|reload)
	"$0" stop
	"$0" start
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?

